package com.pinda.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pinda.base.entity.PdTruck;
import com.pinda.base.truck.TruckDto;
import com.pinda.commonutils.Page;
import com.pinda.commonutils.Result;
import com.pinda.commonutils.StatusCode;


import com.pinda.service.IPdTruckService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 车辆信息表 前端控制器
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd_truck")
public class PdTruckController {

    @Autowired
    IPdTruckService iPdTruckService;



    /**
     * 获取车辆列表
     *
     * @param ids 车辆id列表
     * @return 车辆列表
     */
    @GetMapping("")
    public java.util.List<TruckDto> findAll(@RequestParam(name = "ids", required = false) List<String> ids, @RequestParam(name = "fleetId", required = false) String fleetId) {
        return iPdTruckService.findAll(ids, fleetId).stream().map(pdTruck -> {
            TruckDto dto = new TruckDto();
            BeanUtils.copyProperties(pdTruck, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    /**
     * 添加车俩
     * @param pdTruck
     * @return
     */

    @RequestMapping("/updateTruckByid")
    public Result updateTruckByid(@RequestBody PdTruck pdTruck){
        UpdateWrapper<PdTruck> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", pdTruck.getId()).set("allowable_load", pdTruck.getAllowableLoad()).
                set("license_plate", pdTruck.getLicensePlate()).set("allowable_volume",pdTruck.getAllowableVolume()).
                set("device_gps_id",pdTruck.getDeviceGpsId());
        boolean save = this.iPdTruckService.update(pdTruck,updateWrapper);
        if (save){
            return new Result(true, StatusCode.OK,"添加车俩成功",save);
        }
        return new Result(false , StatusCode.ERROR,"添加车俩失败",save);
    }


    /**
     * 分页条件查询车俩管理
     * @param page
     * @return
     */
    @RequestMapping("/queryPageTruck")
    public Result queryPageTruck(@RequestBody Page<PdTruck> page){
        return this.iPdTruckService.queryPageTruck(page);
    }


    /**
     * 添加车俩
     * @param pdTruck
     * @return
     */

    @RequestMapping("/addTruck")
    public Result addTruck(@RequestBody PdTruck pdTruck){
        pdTruck.setId("T1005");
        boolean save = this.iPdTruckService.save(pdTruck);
        if (save){
            return new Result(true, StatusCode.OK,"添加车俩成功",save);
        }
        return new Result(false , StatusCode.ERROR,"添加车俩失败",save);
    }


    /**
     *根据id删除车俩
     * @param id
     * @return
     */
    @RequestMapping("/deleteTruck")
    public Result deleteTruck(Integer id){
        boolean b = this.iPdTruckService.removeById(id);
        if (b){
            return new Result(true, StatusCode.OK,"删除车俩成功",b);
        }
        return new Result(false , StatusCode.ERROR,"删除车俩失败",b);
    }


}
